'''
Created on May 6, 2013

@author: lord voltron
'''
#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
import sys
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtDeclarative import *
import myAstar


class ImageProvider(QDeclarativeImageProvider):
#    changed = Signal()
    def __init__(self,parent = None):
        QDeclarativeImageProvider.__init__(self, QDeclarativeImageProvider.Image)
        self.parent = parent
    
    def requestImage(self,id,size,requestedSize):
        print "requestImage: ", id
        if "path" in id:
            return self.parent.astar.path

class astarWrapper(QObject):
    def __init__(self):
        QObject.__init__(self)
        self.astar = myAstar.AStar()
        
    @Slot(int,int)
    def setStart(self,x,y):
        self.astar.setStart(x, y)

    @Slot(int,int)
    def setEnd(self,x,y):
        self.astar.setEnd(x, y)
    
    @Slot()
    def updatePath(self):
        self.astar.process()

# Our main window
class MainWindow(QDeclarativeView):
   
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setWindowTitle("Main Window")
        # Renders 'view.qml'
        #com = communicate()
        pathfinder = astarWrapper()
        self.ip = ImageProvider(pathfinder)
        #pathfinder.astar.processDone.connect(self.ip.emitChanged)
        #pathfinder.astar.processDone.connect(self.sepp)
        engine = self.engine()
        engine.addImageProvider("path",self.ip)
        rc = self.rootContext()
        #rc.setContextProperty("communicate",com)
        rc.setContextProperty("pathfinder",pathfinder)
        self.setSource(QUrl.fromLocalFile('view.qml'))
        # QML resizes to main window
        view = self.rootObject()
        self.setResizeMode(QDeclarativeView.SizeRootObjectToView)
        pathfinder.astar.processDone.connect(view.updatePathImage)
            
  
if __name__ == '__main__':
    # Create the Qt Application
    app = QApplication(sys.argv)
    # Create and show the main window
    window = MainWindow()
    window.show()
    # Run the main Qt loop
    sys.exit(app.exec_())